﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Xml;
using System.Configuration;

namespace RecordBeRun
{
    public partial class MainFrom : Form
    {
        UserViewModel ViewModel = new UserViewModel();
        
        public MainFrom()
        {
            InitializeComponent();
            ViewModel = Program.UserViewModelLocator;
            lbx_Mentors.DataSource = ViewModel.AllMentors;
            lbx_Graduate.DataSource = ViewModel.GraduatesUnderMentor;
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var abme =new AboutMe();
            abme.Show();
        }

        private void btn_Run_Click(object sender, EventArgs e)
        {
            try
            {
                
                var user = lbx_Graduate.SelectedItem as Graduate;
                if (user == null)
                {

                }
                else
                {
                    var appConfigRun = ConfigurationSettings.AppSettings["ProgramPath"];
                    System.Diagnostics.Process.Start(appConfigRun);
                    TxtFileHelper.WriteRecord("Data/Record.txt", user.DisplayName+"——"+DateTime.Now.ToString()+"——");
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("无法找到指定的目标程序，请重新确认配置文件！");
            }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btn_AddMentor_Click(object sender, EventArgs e)
        {
            UserAddFrom uaf = new UserAddFrom();
            uaf.ShowDialog();
        }

        private void btn_AddGraduate_Click(object sender, EventArgs e)
        {
            UserAddFrom uaf = new UserAddFrom();
            uaf.StartMode = Mode.Student;
            uaf.ShowDialog();
        }

        private void lbx_Mentors_SelectedIndexChanged(object sender, EventArgs e)
        {
            var mentorId = (Guid)((ListBox)sender).SelectedValue;
            ViewModel.SelectedMentor = (Mentor)((ListBox)sender).SelectedItem;
            lbx_Graduate.DataSource = ViewModel.GraduatesUnderMentor;
        }

        private void btn_DeleteMentor_Click(object sender, EventArgs e)
        {
            var toDeleted = lbx_Mentors.SelectedItem as Mentor;
            string msg = "确认删除 导师 " + toDeleted.DisplayName + " ？";
            DeleteUser du = new DeleteUser(msg);
            
            var dr= du.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
              
               ViewModel.AllMentors.Remove(toDeleted);
            }
        }

        private void btn_DeleteGraduate_Click(object sender, EventArgs e)
        {
            var toDeleted = lbx_Graduate.SelectedItem as Graduate;
            string msg = "确认删除 使用者 " + toDeleted.DisplayName + " ？";
            DeleteUser du = new DeleteUser(msg);
            
            var dr = du.ShowDialog();
            
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                ViewModel.AllGraduates.Remove(toDeleted);
            }
        }

        private void MainFrom_FormClosing(object sender, FormClosingEventArgs e)
        {
            XMLHelper.SaveModelToXmlFile<Graduate>(ViewModel.AllGraduates, "Data/Graduates.xml");
            XMLHelper.SaveModelToXmlFile<Mentor>(ViewModel.AllMentors, "Data/Mentors.xml");
        }
    }
}
